function returnComm() {
	var url = null;
	if (type == "2") {
		var url = "/IMS/community/showList.ht";
		url = url + "?companyId=" + pcid;
	} else {
		var url = "/IMS/community/showList.ht";
	}
	$.go(url, {
		dataType : 'html',
		beforeSend : function() {
			showProccessBar();
		},
		error : function() {
			hideProccessBar();
		},
		success : function(data, textStatus) {
			replaceDocument(url, data);
		}
	});
}

function enterUnit() {
	var url = "/IMS/houseUnit/showList.ht";
	url = url + "?cid=" + cid_;
	$.go(url, {
		dataType : 'html',
		beforeSend : function() {
			showProccessBar();
		},
		error : function() {
			hideProccessBar();
		},
		success : function(data, textStatus) {
			replaceDocument(url, data);
		}
	});
}

function orderSearch(order) {
	if (orderStatus == 1 && orderIndex == order) {
		$("#orderStatus").val(2);
	} else {
		$("#orderStatus").val(1);
	}

	$("#orderIndex").val(order);
	$.submit($('#frm'), {
		dataType : 'html',
		beforeSend : function() {
			showProccessBar();
		},
		error : function() {
			hideProccessBar();
		},
		success : function(data, textStatus) {
			var url = $('#frm').attr('action');
			replaceDocument(url, data);
		}
	});
}

$(function() {
	$("#orderIndex").val(orderIndex);
	$("#orderStatus").val(orderStatus);
	var order = $("#order" + orderIndex);
	if (orderIndex == 1) {
		if (orderStatus == 1) {
			$("#sp" + orderIndex).html("↑");
		} else {
			$("#sp" + orderIndex).html("↓");
		}
	} else if (orderIndex == 2) {
		if (orderStatus == 1) {
			$("#sp" + orderIndex).html("↑");
		} else {
			$("#sp" + orderIndex).html("↓");
		}
	}

	$("input[name=chargeItemIds]").click(function() {
		var temp = $(this);
		if (temp.attr("checked"))
			return;
		var chargeId = $(this).val();
		var _cList = $("input[name=chargeItemIds]:checked");
		$.each(_cList, function() {
			var _cId = $(this).val();
			if (_cId == chargeId)
				return;
			if (chargeId.length > 4) {
				if (chargeId.indexOf(_cId) > -1) {
					$(this).prop("checked", false);
					return;
				}
			} else if (chargeId.length == 4) {
				if (_cId.indexOf(chargeId) > -1) {
					$(this).prop("checked", false);
					return;
				}
			}
		});
	});
});

var dialogIndex;

function edit(id, cid) {
	var url = "/IMS/house/showBaseInfo.ht?hid=" + id + "&cid=" + cid;
	$.go(url, {
		dataType : 'html',
		beforeSend : function() {
			showProccessBar();
		},
		error : function() {
			hideProccessBar();
		},
		success : function(data, textStatus) {
			replaceDocument(url, data);
		}
	});
}

function batchAdd() {
	if ($("#uploadFile")) {
		$.submit($("#uploadForm"), {
			dataType : 'json',
			beforeSend : function() {
				showProccessBar();
			},
			error : function() {
				hideProccessBar();
			},
			success : function(data, textStatus) {
				if (data.code == '1') {
					_alert("导入失败 " + data.message);
					hideProccessBar();
				} else {
					_alert("导入成功", function() {
						if (data.result) {
							$("#downlandFile").attr("href", "/IMS/house/downlandResult.ht?batchId=" + data.result);
							$("#downland_li").show();
						}
						hideProccessBar();
					});
				}
			}
		});
	} else {
		_alert("请选择上传文件");
	}
}

function addRelations() {
	if ($("input[name=chargeItemIds]:checked").length <= 0) {
		parent.layer.msg("请选择费项", {
			time : 1000
		});
		return;
	}
	$.submit($("#itForm"), {
		dataType : 'json',
		beforeSend : function() {
			showProccessBar();
		},
		error : function() {
			hideProccessBar();
		},
		success : function(data, textStatus) {
			if (data.code == '0') {
				closeDialog("ks-pop-box2");
				parent.layer.msg("保存成功");
			} else {
				parent.layer.msg("保存失败");
			}
			hideProccessBar();
		}
	});
}
function openToOwner(pid, cid) {
	if (pid == null || pid == '' || pid == undefined || pid == 'null' || cid == null || cid == '' || cid == undefined || cid == 'null') {
		return;
	} else {
		var url = "/IMS/owner/showList.ht?pid=" + pid + "&communityId=" + cid;
		var title = "业主信息管理";

		var jq = top.jQuery;
		showProccessBar();
		if (jq("#tab-menus").tabs("exists", title)) {
			jq("#tab-menus").tabs("close", title)
		}
		if (!url) {
			hideProccessBar();
			return;
		}
		tab_is_new = true;
		jq("#tab-menus").tabs("add", {
			title : title,
			selected : true,
			closable : true,
			cache : false,
			content : function() {
				var height = document.documentElement.scrollHeight + 'px';
				var dom = "<iframe height=\"" + height + "\" width=\"100%\" src=\"" + url + "\" name=\"iframe_text\" scrolling=\"auto\" frameborder=\"no\" ></iframe>";
				return dom;
			}
		});
	}
}

function checkDel(formId, _url) {
	if ($("#" + formId + " input[name='ids']:checked").length > 1) {
		_alert("房屋关系信息很多，请确认单个删除");
		return;
	}
	delHouse(formId, _url);
}

function delHouse(formId, _url, msg) {
	if (!formId || typeof _url === "String")
		return;
	if (!_url || typeof _url === "String")
		return;
	if (!$("input[name='ids']")) {
		_alert("没有可选项");
		return false;
	}
	if ($("#" + formId + " input[name='ids']:checked").length <= 0) {
		_alert("请选择您要删除的记录");
		return false;
	}
	var idArray = new Array();
	$('input:checkbox[name=ids]:checked').each(function(index) {
		idArray[index] = $(this).val();
	});
	_url = _url + "?ids=" + idArray;
	if (!msg)
		msg = '是否删除所选记录';
	_confirm("<span style='color:#FE6600'>" + msg + "</span>", function() {
		$.ajax({
			type : 'post',
			async : false,
			url : _url,
			dataType : 'json',
			beforeSend : showProccessBar,
			complete : hideProccessBar,
			success : function(value) {
				var code = value.msgCode;
				if (code == message_success_code) {
					var listUrl = '/IMS/house/showList.ht';
					$.go(listUrl, {
						dataType : 'html',
						success : function(data, textStatus) {
							replaceDocument(listUrl, data);
						}
					});
				} else {
					_alert(value.message);
					return;
				}
			},
			error : function(value, a, b) {
				return;
			}
		});
	});
}

function delH(hid) {
	_confirm("<span style='color:#FE6600'>是否删除当前记录</span>", function() {
		var _url = '/IMS/house/delHouse.ht';
		_url = _url + "?ids=" + hid + "&cid=" + cid_;
		$.ajax({
			type : 'post',
			async : false,
			url : _url,
			dataType : 'json',
			beforeSend : showProccessBar,
			complete : hideProccessBar,
			success : function(value) {
				console.log(value)
				//console.log(message_success_code)
				var code = value.code;
				if (code == 0) {
					var listUrl = '/IMS/house/showList.ht';
					listUrl = listUrl + "?cid= " + cid_;
					window.location.href= listUrl;
				} else {
					_alert(value.message);
					return;
				}
			},
			error : function(value, a, b) {
				return;
			}
		});

	});
}

/* ]]> */

/* <![CDATA[ */
var rowNumber = 1;
var lineNumber = 1;
var aryMid = [];
var st = $("#newMap").val();

function closeLayer(obj) {
	$("#" + obj).css("display", "none");
	$("#plotName").val("0");
}

function closeLayer2(obj) {
	var listurl = "/IMS/house/showList.ht?cid=" + cid_ + "&pcid=" + pcid;
	$.go(listurl, {
		dataType : 'html',
		success : function(data, textStatus) {
			replaceDocument(listurl, data);
		}
	});
	$("#" + obj).css("display", "none");
}

// 清空列表
function clearTableByCid() {
	var st = $("#categoryTable tr").length;
	for (var i = 1; i <= st; i++) {
		if ($("#categoryTable tr").length > 2) {
			document.getElementById("categoryTable").deleteRow(2);
		}
	}
	$("#s1").html("<option value='0'>--选择楼栋--</option>");
	$("#s2").html("<option value='0'>--选择单元--</option>");
	$("#s3").html("<option value='0'>--选择房号--</option>");

	var st = $("#categoryTable tr").length;
	for (var i = 1; i <= st; i++) {
		if ($("#categoryTable tr").length > 2) {
			document.getElementById("categoryTable").deleteRow(2);
		}
	}
	rowNumber = 1;
	lineNumber = 1;
	var parentId = $("#categoryTable").find('tr').eq(1).attr("id");
	var canshu1 = $("#" + parentId + "").find('td').eq(0).find('select').attr("id");
	var canshu2 = $("#" + parentId + "").find('td').eq(1).find('select').attr("id");
	var canshu3 = $("#" + parentId + "").find('td').eq(2).find('select').attr("id");
	var id1 = $("#" + canshu1).parent().attr("id");
	var id2 = $("#" + canshu2).parent().attr("id");
	var id3 = $("#" + canshu3).parent().attr("id");
	$("#" + id1, "#" + id2, "#" + id3).parent().html("");

	$("#" + id1).html("<select class='searchForBiLL' style='width: 112px;' name='s1' id='s1' onchange='init('s1','s2',1)'><option value='0'>--选择楼栋--</option></select>");
	$("#" + id2).html("<select class='searchForBiLL' style='width: 112px;' name='s2' id='s2' onchange='init('s2','s3',2)'><option value='0'>--选择单元--</option></select>");
	$("#" + id3).html("<select class='searchForBiLL hello' style='width: 112px;' name='s3' id='s3'><option value='0'>--选择房号--</option></select>");

}
// 清空列表
function clearTable() {
	var st = $("#categoryTable tr").length;
	for (var i = 1; i <= st; i++) {
		if ($("#categoryTable tr").length > 2) {
			document.getElementById("categoryTable").deleteRow(2);
		}
	}
	rowNumber = 1;
	lineNumber = 1;
	var parentId = $("#categoryTable").find('tr').eq(1).attr("id");
	var canshu1 = $("#" + parentId + "").find('td').eq(0).find('select').attr("id");
	var canshu2 = $("#" + parentId + "").find('td').eq(1).find('select').attr("id");
	var canshu3 = $("#" + parentId + "").find('td').eq(2).find('select').attr("id");
	$("#" + canshu1 + ",#" + canshu2 + ",#" + canshu3 + "").html("");

	$("#" + canshu1).html("<option value='0'>--选择楼栋--</option>");
	$("#" + canshu2).html("<option value='0'>--选择单元--</option>");
	$("#" + canshu3).html("<option value='0'>--选择房号--</option>");
	$("#" + canshu1 + ",#" + canshu2 + ",#" + canshu3 + "").select2();
}

$(function() {

	$(".select2").select2();
	$("#s1,#s2,#s3").select2();
	$("#copyCid").val($("#cid").val());
	$("#themeSubit").click(function() {
		// 判断房屋id非空
		var line = $("#categoryTable tr").length;
		for (var i = 1; i < line; i++) {
			if (i == 1) {
				var hid = "s3";
			} else {
				var hid = "s3" + (i - 1);
			}
			if ($("#" + hid + " option:selected").val() == "0") {
				_alert("请选择房屋");
				return;
			}
		}
		var form = $('#exportForm');
		_alert("账单正在生成，请稍等");
		$.submit(form, {
			dataType : 'html',
			beforeSend : showProccessBar,
			complete : hideProccessBar,
			success : function(data, textStatus) {
				if (data.dataCode == "1") {
					_alert(data.Message);
					var listUrl = "/IMS/house/showList.ht?cid=" + cid_;
					replaceDocument(listUrl, data);
				} else {
					_alert("账单生成成功,请稍等,正在刷新", function() {
						var listUrl = "/IMS/house/showList.ht?cid=" + cid_;
						replaceDocument(listUrl, data);
					});
				}
			}
		});
	});
})
// 刷新页面
function reloadHtml() {
	window.location.reload();
}
// 删除行
function DelRow() {
	if (rowNumber <= 1) {
		_alert("至少保留一个");
		return false;
	}
	var getRow = window.event.srcElement.parentElement.parentElement.rowIndex;
	categoryTable.deleteRow(getRow);
	lineNumber = lineNumber - 1;
	rowNumber = rowNumber - 1;
	$("#rowNumber").val(rowNumber);
}
// 上移一行
function Move_up(src) {
	var rowIndex = src.parentElement.parentElement.rowIndex;
	if (rowIndex >= 2)
		change_row(rowIndex - 1, rowIndex);
}
// 下移一行
function Move_down(src) {
	var rowIndex = src.parentElement.parentElement.rowIndex;
	var tl = document.getElementById("categoryTable");
	if (rowIndex < tl.rows.length - 1)
		change_row(rowIndex + 1, rowIndex);
}
// 交换行数
function change_row(line1, line2) {
	categoryTable.rows[line1].swapNode(categoryTable.rows[line2]);
}
// 获取数组
function getArray(obj) {
	var arr = obj.split(",");
	var nums = [];
	for (var i = 0; i < arr.length; i++) {
		nums.push(arr[i].replace(/(^\s*)|(\s*$)/g, ""));
	}
	var diyige = [];
	var array = [];
	for (var i = 0; i < nums.length; i++) {
		if (i % 6 == 0 || i % 6 == 5) {
		} else {
			diyige.push(nums[i]);
		}
		if (i % 6 == 0 && i != 0) {
			array.push(diyige);
			diyige = [];
		}
	}
	array.push(diyige);
	$("#newMap").val(array);
	aryMid = array;
}
// 去掉数组重复值
function unique(arr) {
	var result = [], isRepeated;
	for (var i = 0; i < arr.length; i++) {
		isRepeated = false;
		for (var j = 0, len = result.length; j < len; j++) {
			if (arr[i] == result[j]) {
				isRepeated = true;
				break;
			}
		}
		if (!isRepeated) {
			result.push(arr[i]);
		}
	}
	return result;
}
// 动态添加行数
function AddNewCategory() {
	var array = aryMid;
	if (rowNumber > 4) {
		_alert("最多添加5个");
		return false;
	}
	var sourceNode = $("#parent");
	var clonedNode = sourceNode.clone(true);
	var parent = "parent" + lineNumber;
	if ($("#" + parent + "").size() > 0) {
		lineNumber = lineNumber + 1;
		parent = "parent" + lineNumber;
	}
	$("#categoryTable").append(
			"<tr id='" + parent + "'><td style='text-align: center; width: 112px;' id='s111" + lineNumber + "'><select class='searchForBiLL' style='width: 112px;' name='s1" + lineNumber + "' id='s1" + lineNumber + "' onchange=\"init('s1" + lineNumber + "','s2" + lineNumber + "',1)\"><option  value='0'>--选择楼栋--</option></select ></td><td id='s222" + lineNumber + "' style='text-align: center; width: 112px;'><select class='searchForBiLL' style='width: 112px;' name='s2" + lineNumber + "' id='s2"
					+ lineNumber + "' onchange=\"init('s2" + lineNumber + "','s3" + lineNumber + "',2)\"><option  value='0'>--选择单元--</option></select style='width: 112px;'></td><td id='s333" + lineNumber + "' style='text-align: center; width: 112px;'><select class='searchForBiLL' style='width: 112px;' name='s3" + lineNumber + "' id='s3" + lineNumber
					+ "'><option value='0'>--选择房号--</option></select></td><td  style='text-align:center;width: 112px;'><input type='button' value='新增' onclick='AddNewCategory()' style='width:80px;'/></td><td  style='text-align:center;width: 112px;'><input type='button' value='删除' onclick='DelRow()' style='width:80px;'/></td></tr>");
	var repeat = [];
	for (var i = 0; i < array.length; i++) {
		// 去掉重复值
		repeat.push(array[i][0]);
	}
	var checkRepeat = unique(repeat);
	for (var i = 0; i < checkRepeat.length; i++) {
		$("#s1" + lineNumber + "").append("<option value='" + checkRepeat[i] + "'>" + checkRepeat[i] + "</option>");
	}
	// 每添加一个，初始化一个
	var initNumber = "s1" + lineNumber;
	lineNumber = lineNumber + 1;
	rowNumber = rowNumber + 1;
	$("#rowNumber").val(lineNumber);
	setTimeout(function() {
		$(".searchForBiLL").select2();
	}, 1);
	return lineNumber;
}
// 三级联动
function init(s1name, s2name, jb) {
	var array = aryMid;
	var checkNode = "";
	var kk = 0;
	var sl2 = document.getElementById(s2name);
	var s1sp = s1name.split("");
	var s3name = "s3";
	if (s1sp.length > 2) {
		for (var i = s1sp.length - 1; i < s1sp.length; i++) {
			s3name = s3name + s1sp[i];
		}
	}
	$("#" + s3name + "").html("");
	$("#" + s3name + "").append("<option value='0'>--选择房号--</option>");
	$("#" + s1name + "").parent().parent();
	var parentNode = $("#" + s1name + "").parent().parent().attr("id");
	if (typeof (parentNode) != "undefined") {
		var checkNodeMid = $("#" + parentNode + "").children().children("").attr("id");
		checkNode = $("#" + checkNodeMid + "").val()
	}
	if (s1name.length == 0) {
		var sa1 = [];
		for (var i = 0; i < array.length; i++) {
			if (sa1.length != 0) {
				var a = 0;
				for (var o = 0; o < sa1.length; o++) {
					if (sa1[o] == array[i][0]) {
						a = -1;
						break;
					}
				}
				if (a == 0) {
					sa1[kk] = array[i][jb];
					kk = kk + 1;
				}
			} else {
				sa1[kk] = array[i][jb];
				kk = kk + 1;
			}
		}
		sa1 = unique(sa1);
		for (var j = 0; j < sa1.length; j++) {
			sl2.options[sl2.length] = new Option(sa1[j], sa1[j]);
		}
	} else {
		var slup = document.getElementById(s1name);// s1节点
		sl2.innerHTML = "";
		var sa = [];
		var pa = [];
		for (var i = 0, j = 0; i < array.length; i++) {
			if ((1 == jb && slup.value == array[i][jb - 1]) || (2 == jb && slup.value == array[i][jb - 1] && checkNode == array[i][jb - 2])) {
				sa[j] = array[i][jb];
				pa[j] = array[i][jb + 1];
				j++;
			}
		}
		var aa = unique(sa);
		if (jb == 1) {
			sl2.options[sl2.length] = new Option("--选择单元--", 0 + "");
		}
		if (jb == 2) {
			sl2.options[sl2.length] = new Option("--选择房号--", 0 + "");
		}
		if (jb == 1) {
			for (var i = 0; i < aa.length; i++) {
				sl2.options[sl2.length] = new Option(aa[i], aa[i]);
			}
		} else if (jb = 2) {
			for (var i = 0; i < aa.length; i++) {
				sl2.options[sl2.length] = new Option(aa[i], pa[i]);
			}
		}
	}
}
/* ]]> */

function exp() {
	var controlUrl = "/IMS/owner/exportExcel.ht";
	var url = controlUrl + "?cid=" + cid_;
	$.ajax({
		url : '/IMS/securityProtect/check.ht',
		data : {
			url : controlUrl
		},
		success : function() {
			window.open(url);
		}
	});
}

// 打开excel页面
function openExcelPage() {
	$("#ks-pop-box3").show();
}

function showFileName() {
	var fileNameText = $("#excelfile").val();
	fileNameText = fileNameText.substring(fileNameText.lastIndexOf('\\') + 1);
	if (fileNameText != '') {
		$("#showFileNameId").text(fileNameText);
	}
}

/* <![CDATA[ */

var searchBatchInterval;
var alert_index;
function importExcel() {
	var excelFile = $("#excelfile").val();
	if (excelFile == '') {
		_alert("请选择需要上传的文件!");
		return false;
	}
	if (excelFile.indexOf('.xls') == -1) {
		_alert("文件格式不正确,请选择正确的Excel文件(后缀名.xls)！");
		return false;
	}
	$.submit($('#excelfrm'), {
		dataType : 'json',
		enctype : 'multipart/form-data',
		beforeSend : showProccessBar,
		complete : hideProccessBar,
		success : function(data, textStatus) {
			var code = data.code;
			var message = data.message;
			if (code == 0) {
				$("#batchNum").val(message);
				alert_index = alertMsg('', '正在处理,请勿关闭页面', 4);
				searchBatchInterval = self.setInterval("searchValidatorResult()", 15000);
				return;
			} else if (code == -1 || code == -2) {
				alertMsg('', message, 5);
			}
		}
	});
}
function searchValidatorResult() {
	var batchNum = $("#batchNum").val();
	$.ajax({
		type : "POST",
		url : "/IMS/pdsHousePrietor/search.ht",
		data : {
			batchNum : batchNum
		},
		success : function(data, status) {
			clearInterval(searchBatchInterval);
			layer.close(alert_index);
			var dataCode = data.code;
			if (dataCode == 0) {
				setValidatorResult(data);
			}
		},
	});

}

function setValidatorResult(data) {
	if (data.result != null) {
		var rvrDtoList = data.result.rvrDtoList;
		var isSuccess = data.result.isSuccess;
		var message = data.message;

		if (isSuccess == "err") {
			var errorRowSize = rvrDtoList.length;
			$("#importHistoricalPrepaidResultDiv").show();
			$("#importHistoricalPrepaidResultTab tr:not(:first)").empty();
			for (var i = 0; i < errorRowSize; i++) {
				var errorRow = rvrDtoList[i];
				// 行号
				var rowNum = errorRow.rowMarker;
				rowNum = parseInt(rowNum);
				if (!isNaN(rowNum)) {
					rowNum = rowNum + 1;
				}

				var newRow = "<tr><td align='center'>" + rowNum + "</td><td>" + errorRow.errorMsg + "</td></tr>";
				$("#importHistoricalPrepaidResultTab tr:last").after(newRow);
			}
			$("#allImportCount").html(data.result.totalCount);
			$("#successImportCount").html(data.result.totalCount - errorRowSize);
			$("#failedImportCount").html(errorRowSize);
		} else if (isSuccess == "fail") {
			alertMsg('错误提示', message, 5);
		} else {
			$("#importHistoricalPrepaidResultDiv2").show();
			$("#allImportCount2").html(data.result.totalCount);
			$("#successImportCount2").html(data.result.totalCount);
		}

	}
}

/* ]]> */

function searchSubmit() {
	/*
	var search = $("#search").val();
	if (Is_Null(search)) {
		_alert("请输入查询条件后再进行搜索");
		return;
	}
	*/
	$.submit($('#frm'), {
		dataType : 'html',
		beforeSend : showProccessBar,
		complete : hideProccessBar,
		success : function(data, textStatus) {
			var url = $('#frm').attr('action');
			replaceDocument(url, data);
		}
	});
}